package com.an.blog.utils;

import com.an.blog.admin.domain.vo.BlogAlbumVo;
import com.an.blog.service.IBlogAlbumService;
import com.an.common.utils.StreamUtils;
import com.an.common.utils.spring.SpringUtils;
import com.an.system.domain.vo.SysOssVo;
import com.an.system.service.ISysOssService;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * 转换工具
 *
 * @author: Ann
 * @createDate: 2023/05/10 21:11:53
 * @version: 1.0.0
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ConvertUtil {

    private static final ISysOssService OSS_SERVICE = SpringUtils.getBean(ISysOssService.class);
    private static final IBlogAlbumService ALBUM_SERVICE = SpringUtils.getBean(IBlogAlbumService.class);


    /**
     * 图像类型转换
     *
     * @param records  记录
     * @param getOssId 获取OSS ID
     * @return {@link Map }<{@link Long }, {@link SysOssVo }>
     * @author Ann
     * @date 2024/06/05
     */
    public static <E> Map<Long, SysOssVo> convert2OssIdSysOssMap(Collection<E> records, Function<E, Long> getOssId) {
        Set<Long> ossIds = StreamUtils.toSet(records, getOssId);
        return StreamUtils.toIdentityMap(OSS_SERVICE.listByIds(ossIds), SysOssVo::getOssId);
    }


    /**
     * 转换相册id为名称
     *
     * @param records  记录
     * @param function 函数
     * @return {@link Map }<{@link Long }, {@link BlogAlbumVo }>
     * @author: Ann
     * @createDate: 2023/05/10 21:21:14
     * @version: 1.0.0
     */
    public static <E> Map<Long, BlogAlbumVo> convertAlbumId2AlbumName(Collection<E> records, Function<E, Long> function) {
        Set<Long> albumIds = records.stream()
            .map(function)
            .collect(Collectors.toSet());
        return ALBUM_SERVICE.queryListByIds(albumIds).stream()
            .collect(Collectors.toMap(BlogAlbumVo::getAlbumId, Function.identity()));
    }
}
